const target = {
    _id: '1024',
    name: 'vuejs'
}

const validators = {
    name(val) {
        return typeof val === 'string' ? true : false
    },
    _id(val) {
        return typeof val === 'number' ? true : false
    }
}

const createValidator = (target, validator) => {
    return new Proxy(target, {
        set(target, propKey, value, proxy) {
            //propKey会自动转string,不用加""
            let bol = validator[propKey](value)
            if (bol) {
                console.log(`属性${propKey}设置OK`);
                Reflect.set(target, propKey, value, proxy)
            } else {
                throw Error(`属性${propKey}设置类型不符`)
            }
        }
    })
}


const proxy = createValidator(target, validators)

proxy.name = 'vue-js.com' // vue-js.com
// proxy.name = 10086 // Uncaught Error: Cannot set name to 10086. Invalid type.
proxy._id = 1025 // 1025
proxy._id = 22

console.log(proxy);